package com.hhhn.wk.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface.OnCancelListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.hhhn.wk.R;
import com.hhhn.wk.login.perfect.Adress;
import com.hhhn.wk.widget.adapter.MyAdaper;
import com.hhhn.wk.widget.adapter.ViewHolder;

import java.util.ArrayList;

public class AddressDialog01 {
	static ArrayList<Adress> adresses01=new ArrayList<>();
	static String title="";
	static MyAdaper<Adress> adaper=null;
	public interface OnActionSheetSelected {
		void onClick(int whichButton,String id,String name);
	}

	private AddressDialog01() {
	}

	public static Dialog showSheet(Context context,String titlet,ArrayList<Adress> adresse, final OnActionSheetSelected actionSheetSelected,
			OnCancelListener cancelListener) {
		adresses01=adresse;
		title=titlet;
		final Dialog dlg = new Dialog(context, R.style.ActionSheet);
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_item01, null);
		final int cFullFillWidth = 10000;
		layout.setMinimumWidth(cFullFillWidth);

		TextView mtitle = layout.findViewById(R.id.title);
		mtitle.setText(title);
		ListView mListView = layout.findViewById(R.id.mListView);

		adaper=new MyAdaper<Adress>(adresses01,R.layout.item_dialgo) {
			@Override
			public void bindView(ViewHolder holder, Adress obj, int position) {
				holder.setText(R.id.tv_tv,obj.getName());
			}
		};
		mListView.setAdapter(adaper);
		adaper.notifyDataSetChanged();


//		TextView mCancel = (TextView) layout.findViewById(R.id.cancel);

//		mContent.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				actionSheetSelected.onClick(0);
//				dlg.dismiss();
//			}
//		});
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
				actionSheetSelected.onClick(1,adresses01.get(i).getId(),adresses01.get(i).getName());
				dlg.dismiss();
			}
		});
//		mListView.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//
//				dlg.dismiss();
//			}
//		});

		Window w = dlg.getWindow();
		WindowManager.LayoutParams lp = w.getAttributes();
		lp.x = 0;
		final int cMakeBottom = -1000;
		lp.y = cMakeBottom;
		lp.gravity = Gravity.BOTTOM;
		dlg.onWindowAttributesChanged(lp);
		dlg.setCanceledOnTouchOutside(false);
		if (cancelListener != null)
			dlg.setOnCancelListener(cancelListener);

		dlg.setContentView(layout);
		dlg.show();

		return dlg;
	}

}
